// pages/advertising/advertising.js

//获取应用实例
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    webUrl: '',
    JSONEncodeURI: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const that = this;
    console.log(options)
    if (options.webUrl) {
      console.log(options.webUrl)
      this.setData({
        JSONEncodeURI: options.webUrl
      })
      let url = JSON.parse(options.webUrl)
      let webUrl = decodeURIComponent(url.url)
      console.log(webUrl)
      var index = '?'
      if (webUrl.indexOf(index) > -1) {
        that.setData({
          webUrl: webUrl + '#wechat_redirect'
        })
      } else {
        that.setData({
          webUrl: webUrl
        })
      }
    }
    try {
      var value = wx.getStorageSync('webUrl')
      console.log(value)
      if (value) {
        // Do something with return value
        if (that.options.from === 'bindMobile') {
          that.setData({
            webUrl: value
          })
        } else {
          var index = '?'
          let url = value
          if (url.indexOf(index) > -1) {
            that.setData({
              webUrl: value + '#wechat_redirect'
            })
          } else {
            that.setData({
              webUrl: value
            })
          }
        }
        wx.removeStorage({
          key: 'webUrl',
          success: function (res) {}
        })
      }
    } catch (e) {
      // Do something when catch error
    }
    if (this.options.mobile && this.options.mobile != '' && this.options.mobile != undefined) {
      const mobile = this.options.mobile
      try {
        wx.setStorageSync('wj_sharingId', mobile)
      } catch (e) {}
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    if (app.globalData.userInfo && app.globalData.userInfo.member) {
      let memberId = app.globalData.userInfo.member.id
      return {
        title: '无界门店商城让生活更便利',
        path: '/pages/mallModule/advertising/advertising?webUrl=' + this.data.JSONEncodeURI + '&mobile=' + memberId,
        imageUrl: 'https://app-1256684088.cos.ap-beijing.myqcloud.com/wujieImages/iconCover(1).png',
        success(e) {}
      }
    } else {
      return {
        title: '无界门店商城让生活更便利',
        path: '/pages/mallModule/advertising/advertising?webUrl=' + this.data.JSONEncodeURI,
        imageUrl: 'https://app-1256684088.cos.ap-beijing.myqcloud.com/wujieImages/iconCover(1).png',
        success(e) {}
      }
    }
  }
})